package main

type AppInfo struct {
	AppID             string `json:"login_name"`
	AllowedContainers string `json:"docker_id"`
}

func (a *AppInfo) AuthContainer(cid string) bool {
	id := a.AllowedContainers
	if cid == id {
		return true
	}
	return false
}

func ParseCommandAndCid(appid string, input []string) (output []string, cid string) { //解析ssh命令行  并转换成宿主机的命令行
	if len(input) < 1 {
		return
	}

	if input[0] == "bash" {
		output = []string{"sudo", "-u", appid, "bash"}
		return
	}
	if input[0] == "ps" {
		output = append([]string{"docker"}, input...)
		return
	}
	if len(input) == 2 {
		if cmd := input[0]; cmd == "start" || cmd == "stop" || cmd == "restart" || cmd == "stats" {
			output = append([]string{"docker"}, input...)
			cid = input[1]
		}

	}
	if input[0] == "exec" {
		if len(input) == 4 {
			cid = input[2]
			output = append([]string{"docker"}, input...)
		}
	}
	if input[0] == "logs" {
		if len(input) == 2 {
			cid = input[1]
			output = append([]string{"docker"}, input...)
			return
		} else if len(input) == 3 {
			cid = input[2]
			output = append([]string{"docker"}, input...)
			return
		}
	}
	if input[0] == "rm" {
		if len(input) == 2 {
			cid = input[1]
			output = append([]string{"docker"}, input...)
			return
		} else if len(input) == 3 {
			cid = input[2]
			output = append([]string{"docker"}, input...)
			return
		}
	}
	return

}
